/**
 * 部门管理初始化
 */
var DoctorRevisit = {
    id: "DoctorRevisitTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
DoctorRevisit.initColumn = function () {
    var columns = [
        {field: 'selectItem', radio: true},
        {title: 'customerId', field: 'customerId', visible: false, align: 'center', valign: 'middle'},
        {title: 'id', field: 'id', visible: false, align: 'center', valign: 'middle'},
        {title: 'reservationsId', field: 'reservationsId', visible: false, align: 'center', valign: 'middle'},
        {title: '患者姓名', field: 'customerName', align: 'center', valign: 'middle', sortable: true,
            formatter: function (value, row, index) {
                var ids = row.reservationsId + "," + row.customerId;
                return "<a href='javascript:;' onclick='DoctorRevisit.lookDetail(" + '"' + row.customerId + '"' + ")'>"+row.customerName+"</a>"
            }
        },
        {title: '病历号', field: 'medicalRecordNumber', align: 'center', valign: 'middle', sortable: true},
        {title: '联系电话', field: 'customerPhone', align: 'center', valign: 'middle', sortable: true},
        {title: '预约医生', field: 'doctor', align: 'center', valign: 'middle', sortable: true},
        {title: '咨询师', field: 'fieldTc', align: 'center', valign: 'middle', sortable: true},
        {title: '回访内容', field: 'remarks', align: 'center', valign: 'middle', sortable: true,width: '25%',},
        {title: '回访结果', field: 'revisitContent', align: 'center', valign: 'middle',sortable: true,},
        {title: '回访时间', field: 'revisitTime', align: 'center', valign: 'middle', sortable: true,
            formatter: function (value, row, index) {
               return value.substring(0,11)
            }
        },
        {title: '回访状态', field: 'revisitStatus', align: 'center', valign: 'middle', sortable: true,
            formatter: function (value, row, index) {
                if (value == 1) {
                    return "待计划";
                } else if (value == 2) {
                    return "回访完成"
                }
            }
        },
        {title: '操作', field: 'id', align: 'center', valign: 'middle', sortable: true,
            formatter: function (value, row, index) {
                var ids = row.id+","+row.reservationsId + "," + row.customerId +","+row.revisitTime;
                return '<button title="编辑随访" onclick="DoctorRevisit.followRevision(' + "'" + ids + "'" + ')" type="button" class="btn" style="background: #ffffff;border: 1px solid #cccccc;"><span class="glyphicon glyphicon-pencil"></span></button>'
            }
        }
    ];
    return columns;
};

/**
 * 点击姓名查看详情
 */
DoctorRevisit.lookDetail = function (id) {
    var index = layer.open({
        type: 2,
        title: '患者信息',
        area: ['100%', '100%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/doctorLooked/doctor_seeDiagnosis/' + id
    });
    this.layerIndex = index;
};

/**
 * 随访
 */
DoctorRevisit.followRevision = function (ids) {
    var index = layer.open({
        type: 2,
        title: '随访',
        area: ['60%', '100%'], //宽高
        fix: true, //不固定
        maxmin: true,
        offset:['0px','40%'],
        content: Feng.ctxPath + '/doctorRevisit/doctor_follow/'+ids
    });
    this.layerIndex = index;
};

/**
 * 市场录入详情
 */
DoctorRevisit.openscDetail = function (reservationsId) {
    var index = layer.open({
        type: 2,
        title: '预约客户详情',
        area: ['90%', '90%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/reservations/reservations_details/' + reservationsId
    });
    this.layerIndex = index;
};

/**
 * 前台录入详情
 */
DoctorRevisit.openqtDetail = function (id) {
    var index = layer.open({
        type: 2,
        title: '预约客户详情',
        area: ['90%', '90%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/customer/customer_details/' + id
    });
    this.layerIndex = index;
};


DoctorRevisit.openAddVisited = function (reservationsId) {
    var index = layer.open({
        type: 2,
        title: '添加回访记录',
        area: ['90%', '90%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/DoctorRevisit/to_addVisited/' + reservationsId
    });
    this.layerIndex = index;
};


DoctorRevisit.openRevisitRecord = function (reservationsId) {
    var index = layer.open({
        type: 2,
        title: '回访记录',
        area: ['90%', '90%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/DoctorRevisit/to_revisitRecord/' + reservationsId
    });
    this.layerIndex = index;
};

DoctorRevisit.updateDiagnosisDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '看诊详情',
            area: ['90%', '90%'], //宽高
            fix: true, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/DoctorRevisit/DoctorRevisit_update/' + DoctorRevisit.seItem.id
        });
        this.layerIndex = index;
    }
};

/**
 * 打开看诊详情
 */
DoctorRevisit.setDiagnosisRecord = function (reservationsId) {
    var index = layer.open({
        type: 2,
        title: '看诊记录',
        area: ['90%', '90%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/DoctorRevisit/diagnosis_details/' + reservationsId
    });
    this.layerIndex = index;
};



/**
 * 检查是否选中
 */
DoctorRevisit.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length == 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        DoctorRevisit.seItem = selected[0];
        return true;
    }
};

/**
 * 查询预约客户列表
 */
DoctorRevisit.search = function () {
    DoctorRevisit.table.refresh({query: DoctorRevisit.formParams()});
};

DoctorRevisit.resetSearch = function () {
    $("#reach").val("");
    $("#revisitTime").val("");
    document.getElementById("revisitTime").value = myDate.getFullYear() + "-" + month + "-" + day ;
    DoctorRevisit.inquire();
};

DoctorRevisit.inquire = function () {
    DoctorRevisit.search();
    DoctorRevisit.total();
}

/**
 * 统计
 */
DoctorRevisit.total = function () {
    var param1 = $("#revisitTime").val();  //回访时间
    $.ajax({
        type: "GET",
        url: Feng.ctxPath + "/doctorRevisit/total",
        data: {
            param1: param1
        },
        dataType: "json",
        success: function (data) {
            $("#all").html(data[0].countNum);//总人数
            $("#unfinished").html(data[1].countNum);//未完成
            $("#finished").html(data[2].countNum);//已完成
        }
    });
};

/**
 * 查询表单提交参数对象
 * @returns {{}}
 */
DoctorRevisit.formParams = function () {
    var queryData = {};
    queryData['reach'] = $("#reach").val();
    queryData['revisitTime'] = $("#revisitTime").val();
    return queryData;
};

var myDate = new Date();
var month;
var day;
if ((myDate.getMonth() + 1) < 10) {
    month = "0" + (myDate.getMonth() + 1);
} else {
    month = "" + (myDate.getMonth() + 1);
}
if (myDate.getDate() < 10) {
    day = "0" + myDate.getDate();
} else {
    day = "" + myDate.getDate();
}
$("#next").click(function () {
    var stime = document.getElementById("revisitTime").value;
    var date = new Date(stime);
    var monthe;
    var daye;
    if (stime.length == 7) {
        date.setMonth(date.getMonth() + 1);
        if ((date.getMonth() + 1) < 10) {
            monthe = "0" + (date.getMonth() + 1);
        } else {
            monthe = "" + (date.getMonth() + 1);
        }
        document.getElementById("revisitTime").value = date.getFullYear() + "-" + monthe;
    }
    if (stime.length == 10) {
        date.setDate(date.getDate() + 1);
        if ((date.getMonth() + 1) < 10) {
            monthe = "0" + (date.getMonth() + 1);
        } else {
            monthe = "" + (date.getMonth() + 1);
        }
        if (date.getDate() < 10) {
            daye = "0" + date.getDate();
        } else {
            daye = "" + date.getDate();
        }
        //// 加一天
        document.getElementById("revisitTime").value = date.getFullYear() + "-" + monthe + "-" + daye;
    }
})

$("#minus").click(function () {
    var stime = document.getElementById("revisitTime").value;
    var date = new Date(stime);
    var monthe;
    var daye;
    if (stime.length == 7) {
        date.setMonth(date.getMonth() - 1);
        if ((date.getMonth() + 1) < 10) {
            monthe = "0" + (date.getMonth() + 1);
        } else {
            monthe = "" + (date.getMonth() + 1);
        }
        document.getElementById("revisitTime").value = date.getFullYear() + "-" + monthe;
    }
    if (stime.length == 10) {
        date.setDate(date.getDate() - 1);
        if ((date.getMonth() + 1) < 10) {
            monthe = "0" + (date.getMonth() + 1);
        } else {
            monthe = "" + (date.getMonth() + 1);
        }
        if (date.getDate() < 10) {
            daye = "0" + date.getDate();
        } else {
            daye = "" + date.getDate();
        }
        //// 加一天
        document.getElementById("revisitTime").value = date.getFullYear() + "-" + monthe + "-" + daye;
    }
})
$(function () {
    document.getElementById("revisitTime").value = myDate.getFullYear() + "-" + month + "-" + day;
    var stime = document.getElementById("revisitTime").value;
    var mtime = stime.substring(0, 7)
    $(".btn2").click(function () {
        $("#revisitTime").val(mtime)
        $("#revisitTime").attr("disabled", true)
    })
    $(".btn3").click(function () {
        $("#revisitTime").val(stime)
        $("#revisitTime").attr("disabled", false)
    })
    var defaultColunms = DoctorRevisit.initColumn();
    var table = new BSTable(DoctorRevisit.id, "/doctorRevisit/list", defaultColunms);
    table.setPaginationType("server");
    table.setQueryParams(DoctorRevisit.formParams());
    DoctorRevisit.table = table.init();
    DoctorRevisit.search();
    DoctorRevisit.total();
    //采用定时器监听 时间内容是否变化
    var oldStartValue = $("#revisitTime").val();
    setInterval(
        function () {//定时执行方法内容
            if ($("#revisitTime").val() != oldStartValue) {//如果时间发生变化，就触发查询事件
                DoctorRevisit.search();
                DoctorRevisit.total();
                //如果值发生变化之后，把最新的值赋值到老的变量里面
                oldStartValue = $("#revisitTime").val();
            }
        }, 500);
});
